package com.echo.util.impl;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

public class DateUtils {

	public static final String PATTERN_1 = "yyyy-MM-dd HH:mm:ss";

	public static final String PATTERN_2 = "yyyy-MM-dd";

	public static final String PATTERN_3 = "HH:mm";

	public static final String PATTERN_4 = "M月d日";

	public static final String PATTERN_5 = "MM/dd/yyyy";

	public static final String PATTERN_6 = "yyyy-MM-dd HH:mm";

	/**
	 * yyyy-MM-dd HH:mm格式的时间转换成long型的时间戳
	 * @param dateStr
	 * @return
	 */
	public static long str2DateLong(String dateStr) {
		try {
			return callbackDateFormat(PATTERN_6).parse(dateStr).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return -1;
	}

	/**
	 * MM/dd/yyyy格式的时间格式转换成时间戳
	 * 
	 * 
	 */
	public static long str2DateL_new(String dateStr) {
		try {
			return callbackDateFormat(PATTERN_5).parse(dateStr).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return -1;
	}
	/**
	 * yyyy-MM-dd HH:mm:ss格式的时间转换成long形的时间戳
	 * @param dateStr
	 * @return
	 */

	public static long str2DateL(String dateStr) {
		try {
			return callbackDateFormat(PATTERN_1).parse(dateStr + " 00:00:00").getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return -1;
	}

	/**
	 * 10位字符串日期返回参数日期当天最大时间毫秒数
	 * 
	 * @param dateStr
	 *            "yyyy-MM-dd" 2004-12-21
	 * @return 参数日期当天最大时间毫秒数
	 */
	public static long str2EDateL(String dateStr) {
		try {
			return callbackDateFormat(PATTERN_1).parse(dateStr + " 23:59:59").getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return -1;
	}

	/**
	 * long 数字时间转换为 yyyy-MM-dd 格式字符串
	 * 
	 * @param milltime
	 * @return
	 */
	public static String long2Str(Long milltime, String pattern) {
		Calendar cal = Calendar.getInstance(Locale.CHINA);
		if (milltime == null) {
			return null;
		} else {
			cal.setTimeInMillis(milltime);
			return callbackDateFormat(pattern).format(cal.getTime());
		}
	}
	public static Long currTimeLong(){
		Calendar cal = Calendar.getInstance(Locale.CHINA);
		return cal.getTimeInMillis();
	}
	public static DateFormat callbackDateFormat(String pattern) {
		DateFormat dateFormat = new SimpleDateFormat(pattern);
		dateFormat.setLenient(false);
		return dateFormat;
	}
	public static Long str2Long(String dateStr,String pattern){
		try {
			return callbackDateFormat(pattern).parse(dateStr).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

}
